package cn.lili.modules.utils;

import cn.lili.common.exception.ServiceException;
import cn.lili.modules.operationcenter.entity.enums.DateTypeEnum;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    /**
     * 快捷搜索，得到开始时间和结束时间
     *
     * @param dateTypeEnum
     * @return
     */
    public static Date[] getDateArray(DateTypeEnum dateTypeEnum) {
        Date[] dateArray = new Date[2];
        LocalDateTime currentDateTime = LocalDateTime.now();
        switch (dateTypeEnum) {
            case THIS_WEEK -> {
                // 本周
                LocalDateTime startOfWeek = currentDateTime.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).withHour(0).withMinute(0).withSecond(0);
                LocalDateTime endOfWeek = currentDateTime.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)).withHour(23).withMinute(59).withSecond(59);
                dateArray[0] = Date.from(startOfWeek.atZone(ZoneId.systemDefault()).toInstant());
                dateArray[1] = Date.from(endOfWeek.atZone(ZoneId.systemDefault()).toInstant());
            }
            case LAST_WEEK -> {
                // 上周
                LocalDateTime startOfLastWeek = currentDateTime.minusWeeks(1).with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).withHour(0).withMinute(0).withSecond(0);
                LocalDateTime endOfLastWeek = currentDateTime.minusWeeks(1).with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)).withHour(23).withMinute(59).withSecond(59);
                dateArray[0] = Date.from(startOfLastWeek.atZone(ZoneId.systemDefault()).toInstant());
                dateArray[1] = Date.from(endOfLastWeek.atZone(ZoneId.systemDefault()).toInstant());
            }
            case THIS_MONTH -> {
                // 本月
                LocalDateTime startOfMonth = currentDateTime.with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0);
                LocalDateTime endOfMonth = currentDateTime.with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59);
                dateArray[0] = Date.from(startOfMonth.atZone(ZoneId.systemDefault()).toInstant());
                dateArray[1] = Date.from(endOfMonth.atZone(ZoneId.systemDefault()).toInstant());
            }
            case LAST_MONTH -> {
                //上月
                LocalDateTime startOfLastMonth = currentDateTime.minusMonths(1).with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0);
                LocalDateTime endOfLastMonth = currentDateTime.minusMonths(1).with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59);
                dateArray[0] = Date.from(startOfLastMonth.atZone(ZoneId.systemDefault()).toInstant());
                dateArray[1] = Date.from(endOfLastMonth.atZone(ZoneId.systemDefault()).toInstant());
            }
            case THIS_QUARTERLY -> {
                //本季度
                LocalDateTime startOfQuarter = currentDateTime.with(Month.from(currentDateTime).firstMonthOfQuarter()).with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0);
                LocalDateTime endOfQuarter = currentDateTime.with(Month.from(currentDateTime).firstMonthOfQuarter().plus(2)).with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59);
                dateArray[0] = Date.from(startOfQuarter.atZone(ZoneId.systemDefault()).toInstant());
                dateArray[1] = Date.from(endOfQuarter.atZone(ZoneId.systemDefault()).toInstant());
            }
            case LAST_QUARTERLY -> {
                //上季度
                LocalDateTime startOfLastQuarter = currentDateTime.minusMonths(3).with(Month.from(currentDateTime).firstMonthOfQuarter()).with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0);
                LocalDateTime endOfLastQuarter = currentDateTime.minusMonths(3).with(Month.from(currentDateTime).firstMonthOfQuarter().plus(2)).with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59);
                dateArray[0] = Date.from(startOfLastQuarter.atZone(ZoneId.systemDefault()).toInstant());
                dateArray[1] = Date.from(endOfLastQuarter.atZone(ZoneId.systemDefault()).toInstant());
            }
            case THIS_YEAR -> {
                // 今年
                LocalDateTime startOfYear = currentDateTime.with(TemporalAdjusters.firstDayOfYear()).withHour(0).withMinute(0).withSecond(0);
                LocalDateTime endOfYear = currentDateTime.with(TemporalAdjusters.lastDayOfYear()).withHour(23).withMinute(59).withSecond(59);
                dateArray[0] = Date.from(startOfYear.atZone(ZoneId.systemDefault()).toInstant());
                dateArray[1] = Date.from(endOfYear.atZone(ZoneId.systemDefault()).toInstant());
            }
            case LAST_YEAR -> {
                // 去年
                LocalDateTime startOfLastYear = currentDateTime.minusYears(1).with(TemporalAdjusters.firstDayOfYear()).withHour(0).withMinute(0).withSecond(0);
                LocalDateTime endOfLastYear = currentDateTime.minusYears(1).with(TemporalAdjusters.lastDayOfYear()).withHour(23).withMinute(59).withSecond(59);
                dateArray[0] = Date.from(startOfLastYear.atZone(ZoneId.systemDefault()).toInstant());
                dateArray[1] = Date.from(endOfLastYear.atZone(ZoneId.systemDefault()).toInstant());
            }
            default -> throw new ServiceException();
        }
        return dateArray;
    }
}
